import time

from src.choice_api.utils import data_str2timestamp, sort_by_first_element


def parse_api_lab_data(data, columns_lst):
    if data is None or "list" not in data:
        return None

    data_lst = data["list"]
    if not data_lst:
        data_lst = []

    # 真实要获取的字段
    # TODO 确认指标是否会更改
    total_columns = ['硫酸根', 'SS', 'COD(+AgNO3)', '硬度', '硫化物', '酸碱度', '碱度', '氯离子', '色度', 'MLVSS', 'MLSS','污泥沉降比', 'TSS', 'ALK', 'VFA', '余氯', '生化需氧量', 'TDS', '硝氮', 'Cond', '粪大肠菌群', 'pH','TP', '氨氮', 'TN', 'TOC', 'COD']

    # columns_mapping = {
    #     "硫酸根": "硫酸根(mg/L)",
    #     "SS": "污水悬浮物(mg/L)",
    #     "COD(+AgNO3)": "需氧量(加硝酸银)",
    #     "硬度": "硬度(mg/L)",
    #     "硫化物": "硫化物",
    #     "酸碱度": "酸碱度",
    #     "碱度": "碱度(mg/L)",
    #     "氯离子": "氯离子(mg/L)",
    #     "色度": "色度(倍)",
    #     "MLVSS": "悬浮固体总量(mg/L)",
    #     "MLSS": "悬浮固体中有机物的数量(mg/L)",
    #     "污泥沉降比": "污泥沉降比(%)",
    #     "TSS": "悬浮固体物质总量(mEq/L)",
    #     "ALK": "厌氧处理系统内碱度(mEq/L)",
    #     "VFA": "挥发性有机酸(mEq/L)",
    #     "余氯": "余氯(mg/L)",
    #     "生化需氧量": "生化需氧量(mg/L)",
    #     "TDS": "总溶解性固体物质(mg/L)",
    #     "硝氮": "硝氮(mg/L)",
    #     "Cond": "电导率(μs/cm)",
    #     "粪大肠菌群": "粪大肠菌群(MPN/L)",
    #     "pH": "pH值(无量纲)",
    #     "TP": "总磷(mg/L)",
    #     "氨氮": "氨氮(mg/L)",
    #     "TN": "总氮(mg/L)",
    #     "TOC": "总有机碳(mg/L)",
    #     "COD": "化学需氧量(mg/L)"
    # }

    if isinstance(columns_lst, str) and columns_lst in total_columns:
        columns_lst = [columns_lst]

    elif isinstance(columns_lst, list) or len(columns_lst) > 0:
        target_correct_columns = []
        for target_column in columns_lst:
            if target_column in total_columns:
                target_correct_columns.append(target_column)
        if len(target_correct_columns) > 0:
            columns_lst = target_correct_columns
        else:
            columns_lst = total_columns
    else:
        columns_lst = total_columns

    # 单表csv解析
    # res_columns = ["time"]
    # res_data = [[x["time"]] for x in company_lst[list(company_lst.keys())[0]]]
    #
    # for k, val_lst in company_lst.items():
    #     for param in columns_lst:
    #         res_columns.append(f"{k} {param_mapping[param]}")
    #         for idx, val in enumerate(val_lst):
    #             res_data[idx].append(val[param])
    # return {
    #     "res_columns": res_columns,
    #     "res_data": res_data,
    # }

    # 单表时序指标形式
    res_columns = []
    res_data = {}

    for row in data_lst:
        if row["detectionIndexName"] not in columns_lst:
            continue

        label = f"实验室检测 {row["sampleName"]} {row["detectionIndexName"]}"
        if label not in res_columns:
            res_columns.append(label)
            res_data[label] = []
        if row["indexValue"] != "未检出":
            res_data[label].append([data_str2timestamp(row["checkTime"]), row["indexValue"]])

    for column in res_columns:
        res_data[column] = sort_by_first_element(res_data[column])


    return {
        "res_columns": res_columns,
        "res_data": res_data
    }
